package io.renren.modules.upload;

import io.renren.common.utils.R;
import io.renren.common.utils.SnowFlake;
import io.renren.common.utils.UploadUtil;
import io.renren.modules.teacher.service.CourseStudentManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestBody;

@RestController
@RequestMapping("/upload")
public class UploadController {
    @Autowired
    private UploadDao uploadDao;
    /**
     * 上传图片
     * @param file
     * @return
     */
    @PostMapping("/uploadImg")
    public R imgUpload(@RequestParam(value = "file") MultipartFile file) {
        SnowFlake snowFlake = new SnowFlake(3L,10L);
        try {
            String id = snowFlake.nextId()+"";
            String savePath = UploadUtil.saveFile(file, "imgs/"+id, UploadUtil.FileType.IMG_FILE);
            return R.ok().put("uid",id).put("url", savePath).put("name",file.getOriginalFilename());
        } catch (IllegalArgumentException e) {
            return R.error("请传入正确的类型ID");
        } catch (IOException e) {
            return R.error(e.getMessage());
        }
    }
    //用于上传file音频
    @PostMapping("/uploadBlob")
    public R uploadBlob(@RequestParam(value = "file") MultipartFile file) {
        System.out.println(file.getOriginalFilename());
        System.out.println(file.getName());
        SnowFlake snowFlake = new SnowFlake(3L,10L);
        try {
            String id = snowFlake.nextId()+"";
            String savePath = UploadUtil.saveFile(file, "blob/"+id, UploadUtil.FileType.ANY_FILE);
            return R.ok().put("uid",id).put("url", savePath).put("name",file.getOriginalFilename());
        } catch (IllegalArgumentException e) {
            return R.error("请传入正确的类型ID");
        } catch (IOException e) {
            return R.error(e.getMessage());
        }
    }
    //保存音频地址
    @PostMapping("/updateUrl")
    public R updateUrl(@RequestBody Map<String, Object> formData){
        //删除之前保存状态为1的content内容
        uploadDao.deleteAllContent(formData);
        //让状态为0的改变content的状态1
        uploadDao.updateContentStatus(formData);
        uploadDao.UpdateChapter(formData);
        return R.ok();
    }

    
}
